package com.apes.fn.server.servicePlan.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.masterdata.finance.model.CrossIC;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.product.model.ServeType;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Creator: czs
 * CreateDate: 2023-08-02 11:09:06
 * Description: 报价单
 **/
@Slf4j
@Getter
@Setter
@MappedSuperclass
public class QuotationOrderItems extends BaseModel {


    @Column(name = "sort", length = 250)
    @Comment("排序字段")
    private Integer sort;

    @Column(name = "source_items", length = 24)
    @Comment("原方案明细")
    private String sourceItems;

    // 要货前端显示
    @Transient
    private Map requireView;
    // 领用前端显示
    @Transient
    private Map qtyReceiveView;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;


    public void setProductName(String productName) {
        if (productName==null||"".equals(productName.trim()))throw new RuntimeException("商品名称不能为空或空格");
        this.productName = productName;
    }

    @Column(name = "product_name", nullable = false, length = 128)
    @Comment("商品名称")
    private String productName;

    @Column(name = "sale_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double saleQty = 0;

    @Transient
    private double oldSaleQty = 0;

    public double getOldSaleQty() {
        return saleQty;
    }

    @Transient
    private double qtyEndTemp; //前端传过来的终止数量

    @Column(name = "primary_price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("零售价")
    private double primaryPriceUnit = 0;

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售单价")
    private double priceUnit = 0;

    @Column(name = "primary_price_unit_show", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("原销售单价展示")
    private double  primaryPriceUnitShow = 0;

    @Column(name = "pft_price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("优惠单价")
    private double pftPriceUnit = 0;  //通过价格审核后的


    public double getPftPriceUnit(){ //改结果保留两位小数向上取整
        return Arith.roundUp(this.pftPriceUnit,2);
    }

    public void setSalesAmount(double salesAmount) {
        this.salesAmount = salesAmount;
    }

    @Column(name = "sales_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售金额")
    private double salesAmount = 0;

    @Column(name = "discount_rate", nullable = false, columnDefinition = ApesConst.FIELD_ROUND_16_8)
    @Comment("折扣率")
    private double discountRate = 0;
    public double calculateDiscountRate(){
        if (this.getPrimaryPriceUnitShow()==0)return 0;
        try {
            return Arith.round(Arith.div(this.getPftPriceUnit(),this.getPrimaryPriceUnitShow()),4);
        } catch (Exception e) {
            throw new RuntimeException("计算折扣率错误");
        }
    }
    @Column(name = "remarks", length = 400)
    @Comment("备注")
    private String remarks;

    @Transient
    private boolean needInquiry = true;


    @Column(name = "quality_warranty", length = 32)
    @Comment("质保期")
    private String qualityWarranty;

    @Column(name = "quality_mileage", length = 32)
    @Comment("质保里程")
    private String qualityMileage;

    @Column(name = "discount_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("折扣金额")
    private double discountAmount = 0;

    @Transient
    MaintenanceOrder maintenanceOrder;

    @Transient
    private Integer selectFlag;

    @Transient
    private Map addInfo;

    @Transient
    private boolean selected;

    @Transient
    private String inquirySource = "none";



    @Transient
    private Integer serviceStateFlag;
    public Integer getServiceStateFlag(){
         return 1;
    }

    @Transient
    private String uomName;



    /**
     * 计算结算金额
     * 销售数量 - 终止数量 = 卖的数量（质检前，配件的话必须领用 + 终止 =  销售）
     * @return
     */
    public Double balanceAmount(){
        if(this.product != null && this.product.getProductGroup() != null){
            if("20".equals(this.product.getProductGroup().getId())){
                return Arith.round(Arith.mul(this.getSaleQty(),this.getPftPriceUnit()),0);
            }else if("10".equals(this.product.getProductGroup().getId())){
                return Arith.round(Arith.mul(this.getSaleQty(),this.getPftPriceUnit()),0);
            }else{
                throw new RuntimeException("统计结算金额错误");
            }
        }
       return 0.0;
    }

    @Transient
    private Double balanceAmount;
    public Double getBalanceAmount(){
        return balanceAmount();
    }



    @Transient
    private String terminationType = "0";

    @Transient
    private String note;//询价备注说明

    @Transient
    private String oldPicture;//询价旧件图片

    public Double getDiscountRateTemp() {
        return discountRate*100;
    }

    @Transient
    private Double discountRateTemp;//询价旧件图片

}
